package com.rzcoding.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rzcoding.entity.UserProfile;
import com.rzcoding.service.UserProfileService;
import com.rzcoding.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserProfileController {

    @Autowired
    private UserProfileService userProfileService;

    @GetMapping("/load/{username}")
    public UserProfile findUserInfo(@PathVariable("username") String username){
        return userProfileService.findByUsername(username);
    }

    @PostMapping("/login")
    public ResponseEntity login(@RequestBody UserProfile userProfile){
        boolean result = userProfileService.login(userProfile);
        if (result){
            //密码是正确的
            //生成jwt令牌,返回到客户端
            Map<String,String> info = new HashMap<>();
            info.put("username",userProfile.getUsername());
            //基于工具类生成jwt令牌
            String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), userProfile.getUsername(), null);
            info.put("token",jwt);
            return new ResponseEntity<>(new JSONObject().fluentPut("data", info), HttpStatus.OK);
        }else{
            return new ResponseEntity<>(new JSONObject().fluentPut("data", "登录失败"), HttpStatus.BAD_REQUEST);
        }
    }

    /***
     * UserProfile分页条件搜索实现
     * @param userProfile
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}" )
    public ResponseEntity findPage(@RequestBody(required = false)  UserProfile userProfile, @PathVariable  int pageNum, @PathVariable  int pageSize){
        JSONObject data = new JSONObject();
        //调用UserProfileService实现分页条件查询UserProfile
        PageInfo<UserProfile> pageInfo = userProfileService.findPage(userProfile, pageNum, pageSize);
        return new ResponseEntity<>(new JSONObject().fluentPut("data", pageInfo), HttpStatus.OK);
    }

    /***
     * UserProfile分页搜索实现
     * @param pageNum:当前页
     * @param pageSize:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}" )
    public ResponseEntity findPage(@PathVariable  int pageNum, @PathVariable  int pageSize){
        JSONObject data = new JSONObject();
        //调用UserProfileService实现分页查询UserProfile
        PageInfo<UserProfile> pageInfo = userProfileService.findPage(pageNum, pageSize);
        return new ResponseEntity<>(new JSONObject().fluentPut("data", pageInfo), HttpStatus.OK);
    }

    /***
     * 多条件搜索品牌数据
     * @param userProfile
     * @return
     */
    @PostMapping(value = "/search" )
    public ResponseEntity findList(@RequestBody(required = false)  UserProfile userProfile){
        JSONObject data = new JSONObject();
        //调用UserProfileService实现条件查询UserProfile
        List<UserProfile> list = userProfileService.findList(userProfile);
        return new ResponseEntity<>(new JSONObject().fluentPut("data", list), HttpStatus.OK);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public ResponseEntity delete(@PathVariable Long id){
        //调用UserProfileService实现根据主键删除
        userProfileService.delete(id);
        return new ResponseEntity<>(new JSONObject(), HttpStatus.OK);
    }

    /***
     * 修改UserProfile数据
     * @param userProfile
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public ResponseEntity update(@RequestBody  UserProfile userProfile,@PathVariable Long id){
        //设置主键值
        userProfile.setId(id);
        //调用UserProfileService实现修改UserProfile
        userProfileService.update(userProfile);
        return new ResponseEntity<>(new JSONObject(), HttpStatus.OK);
    }

    /***
     * 新增UserProfile数据
     * @param userProfile
     * @return
     */
    @PostMapping
    public ResponseEntity add(@RequestBody   UserProfile userProfile){
        //调用UserProfileService实现添加UserProfile
        userProfileService.add(userProfile);
        return new ResponseEntity<>(new JSONObject(), HttpStatus.OK);
    }

    /***
     * 根据ID查询UserProfile数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable Long id){
        JSONObject data = new JSONObject();
        //调用UserProfileService实现根据主键查询UserProfile
        UserProfile userProfile = userProfileService.findById(id);
        return new ResponseEntity<>(new JSONObject().fluentPut("data", userProfile), HttpStatus.OK);
    }

    /***
     * 查询UserProfile全部数据
     * @return
     */
    @GetMapping
    public ResponseEntity findAll(){
        //调用UserProfileService实现查询所有UserProfile
        return new ResponseEntity<>(new JSONObject().fluentPut("data", userProfileService.findAll()),
                HttpStatus.OK);
    }
}
